<?php

	/**
	 * Created by PhpStorm.
	 * User: mybook-lhp
	 * Date: 18/7/3
	 * Time: 上午11:49
	 */

	namespace app\member\model;

	use think\Db;
	use think\Model as ThinkModel;

	class MemberInfoModel extends ThinkModel
	{
		// 设置当前模型对应的完整数据表名称
		protected $name = 'member_info';

		// 自动写入时间戳
		protected $autoWriteTimestamp = true;


		/**
		 * 获取用户基本信息
		 * @return false|\PDOStatement|string|\think\Collection
		 * @throws \think\db\exception\DataNotFoundException
		 * @throws \think\db\exception\ModelNotFoundException
		 * @throws \think\exception\DbException
		 */
		static public function getMemberInfo($member_id)
		{
			$data = Db::name('member')->alias('m')
				->field('m.phone,mi.*')
				->join('member_info mi', 'mi.member_id = m.id')
				->where('m.id', $member_id)
				->find();

			$data['nickName'] = is_null($data['nickName']) ? '佚名' : $data['nickName'];
			$data['avatar'] = is_null($data['avatar']) ? get_file_path(59) : get_file_path($data['avatar']);
			return $data;
		}

		static public function initmember($member_id)
		{
			if (!self::where('member_id', $member_id)->find())
			{
				return self::create(['member_id' => $member_id]);
			}
		}

		/**
		 * 获取实名认证列表
		 * @param $map
		 * @param $order
		 * @return \think\Paginator
		 * @throws \think\exception\DbException
		 */
		static public function IdentityList($map, $order)
		{
			return Db::name('member m')
				->field('mw.nickName,m.phone,mi.true_name,m.status,mi.*')
				->join('member_info mi', 'm.id = mi.member_id', 'left')
				->join('wechat_member	 mw', 'm.id = mw.member_id', 'left')
				->where($map)->order($order)->paginate();
		}

		/**
		 * 获取实名认证
		 * @param $member_id
		 * @return array|false|\PDOStatement|string|ThinkModel
		 * @throws \think\db\exception\DataNotFoundException
		 * @throws \think\db\exception\ModelNotFoundException
		 * @throws \think\exception\DbException
		 */
		static public function IdentityInfo($member_id)
		{
			return Db::name('member m')
				->field('mw.nickName,m.phone,mi.true_name,m.status,mi.*')
				->join('member_info mi', 'm.id = mi.member_id', 'left')
				->join('wechat_member	 mw', 'm.id = mw.member_id', 'left')
				->where(['m.id' => $member_id])->find();
		}

		/**
		 * 检测是否通过实名
		 */
		static public function TestingIdentity($member_id)
		{
			return self::field('admin_status')->where(['member_id' => $member_id])->find();

		}

		/**
		 * 提交身份信息
		 */
		static public function identityAuthentication($identity)
		{
			static::initmember($identity['member_id']);
			$mps = ['member_id' => $identity['member_id']];
			$identity['admin_status'] = 1;
			$user = self::where($mps)->find();
			if (!empty($user) && $user['admin_status'] == 0)
			{
				$identity['create_time'] = time();
				return self::where($mps)->update($identity) ? 1 : 0;
			} elseif (!empty($user) && $user['admin_status'] == 2)
			{
				$identity['update_time'] = time();
				return self::where($mps)->update($identity) ? 1 : 0;
			}
			return 2;

		}

		/**
		 * 用户ID获取信息
		 * @param null $id
		 * @return array|null
		 */
		static public function getOneByID($id = null)
		{
			return isset($id) ? self::where(['member_id' => $id])->column() : null;
		}

	}
